﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Enterprise.Infrastructure;
using Enterprise.Model.Role;
using Enterprise.Data.Role;
namespace Enterprise.Service.Role
{
    /// <summary>
    /// 角色权限应用服务
    /// </summary>
    public class RolePermissionService
    {
        /// <summary>
        ///  角色权限缓存标记
        /// </summary>
        private static string cacheKey = "Enterprise-RolePermission";

        /// <summary>
        /// 角色权限列表 返回所有权限数据
        /// </summary>
        /// <returns></returns>
        public List<RolePermissionEntity> RolePermissionList()
        {
            if (CacheHelper.Contains(cacheKey))
            {
                return (List<RolePermissionEntity>)CacheHelper.GetCache(cacheKey);
            }
            else
            {
                RolePermissionData data = new RolePermissionData();
                List<RolePermissionEntity> list = data.RolePermissionList();
                CacheHelper.Add(cacheKey, list);
                return list;
            }
        }

        /// <summary>
        /// 角色权限操作 添加、修改、删除
        /// </summary>
        /// <param name="entity">用户实体</param>
        public void RolePermissionExecute(RolePermissionEntity entity)
        {
            string cmd = string.Empty;
            switch (entity.DB_Option_Action)
            {
                case "Insert":
                    cmd = "Insert into SYS_ROLEPERMISSION(RPID,MODULEID,ROLEID,MCODE,PERMISSIONVALUE) values(SEQ_SYS_ROLEPERMISSION.nextval," + entity.MODULEID + "," + entity.ROLEID + ",'" + entity.MCODE + "'," + entity.PERMISSIONVALUE + ")";
                    break;
                case "Delete":
                    cmd = "Delete from SYS_ROLEPERMISSION where ROLEID=" + entity.ROLEID;
                    break;
            }
            if (!string.IsNullOrEmpty(cmd))
            {
                RoleExecuteBySql(cmd);
            }
        }

        /// <summary>
        /// 组织机构操作  
        /// </summary>
        /// <param name="cmd">sql语句</param>
        public void RoleExecuteBySql(string cmd)
        {
            RoleData data = new RoleData();
            data.RoleExecute(cmd);
            CacheHelper.RemoveCache(cacheKey);
        }
    }
}
